package com.origin.utils.screen;

import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.origin.utils.WallpaperUtils;

public class ScreenStatusController {
    static BroadcastReceiver mScreenBroadcastReceiver;
    static Bitmap defaultBitmap;
    static Bitmap actionBitmap;
    public static void setActionBitmap(Bitmap _actionBitmap){
        actionBitmap=_actionBitmap;
    }

    public static void addScreenChangeBroadCast(Context mContext) {

        defaultBitmap = WallpaperUtils.getLockWallpaper(mContext);
        if (mScreenBroadcastReceiver == null) {
            mScreenBroadcastReceiver = new BroadcastReceiver() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void onReceive(Context context, Intent intent) {

                    String action = intent.getAction();
                    Log.e("LockScreenActivity", "action  " + action);
                    if (action.equals(Intent.ACTION_SCREEN_ON)) {
                        Log.e("LockScreenActivity", "开屏");
                    } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
                        Log.e("LockScreenActivity", "解锁");
                        WallpaperUtils.setLockWallPaper(mContext,defaultBitmap);
                    } else {
                        Log.e("LockScreenActivity", "关闭");
                        WallpaperUtils.setLockWallPaper(mContext,actionBitmap);
                    }
                }
            };
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            filter.addAction(Intent.ACTION_USER_PRESENT);
            try {
                mContext.registerReceiver(mScreenBroadcastReceiver, filter);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public static void removeScreenChangeBroadCast(Context mContext) {
        WallpaperUtils.setLockWallPaper(mContext,defaultBitmap);
        if (mScreenBroadcastReceiver != null) {
            try {
                mContext.unregisterReceiver(mScreenBroadcastReceiver);
            } catch (Exception e) {
                e.printStackTrace();
            }
            mScreenBroadcastReceiver = null;
        }
    }

}
